<template>
	<el-button :disabled='timer !== 0'
		:type='theme'
		@click='sendCode'>{{codeText}}</el-button>
</template>

<script>
	import {cbs, phoneReg} from '../../../config/settings.js';
	module.exports = {
		data() {
			return {
				timer: 0,
				timerCal: ''
			}
		},
		props: {
			theme: {
				type: String,
				default: 'primary'
			},
			phone: {
				type: String,
				default: ''
			},
			limit: {
				type: Number,
				default: 60
			},
			type: {
				type: Number,
				default: 0          // 0-注册 1-忘记密码（未登录） 2-找回支付密码（已登录）
			}
		},
		computed: {
			codeText() {
				return this.timer > 0 ? `请${this.timer}秒后重发` : '发送验证码';
			}
		},
		methods: {
			/**
	         * 发送验证码
	         */
			sendCode() {
				if(!phoneReg.test(this.phone)) {
					this.$emit('errCode', '请填写有效手机号');
				}
				let data = this.$$formData({
					phone: this.phone
				})

				this.timer = this.limit;
				this.timerCal = setInterval(() => {
					--this.timer;
					if(this.timer <= 0) {
						clearInterval(this.timerCal);
					}
				}, 1000);

				switch(this.type) {
					case 0:
						this.onRegister(data);
						break;
					case 1: 
						this.onAccountPwd(data);
						break;
					case 2:
						this.onPayPwd(data);
						break;
					default:
						this.onRegister(data);
				}
			},
			/**
	         * 发送验证码-注册
	         */
			onRegister(data) {
				this.$$api_authen_sendCode(data, res => {
					if(res.event === 0) {
						this.$emit('successCode');
					}else {
						cbs.submitError.call(this, this.errMsg(res.event, res.msg));
						clearInterval(this.timerCal);
						this.timer = res.event === 1002 || res.event === 2 ? -1 : 0;
						this.$emit('errCode', res.msg);
					}
					this.$emit('cbCode', res.event);
				}, {
					tokenFlag: true
				})
			},
			/**
	         * 发送验证码-忘记密码
	         */
			onAccountPwd(data) {
				this.$$api_user_getCode(data, res => {
					if(res.event === 0) {
						this.$emit('successCode');
					}else {
						cbs.submitError.call(this, this.errMsg(res.event, res.msg));
						clearInterval(this.timerCal);
						this.timer = res.event === 1 || res.event === 2 ? -1 : 0;
						this.$emit('errCode', res.msg);
					}
					this.$emit('cbCode', res.event);
				}, {
					tokenFlag: true
				})
			},
			/**
	         * 发送验证码-找回支付密码
	         */
			onPayPwd(data) {
				this.$$api_manager_getCode(data, res => {
					if(res.event === 0) {
						this.$emit('successCode');
					}else {
						cbs.submitError.call(this, this.errMsg(res.event, res.msg));
						clearInterval(this.timerCal);
						this.timer = res.event === 1 || res.event === 2 ? -1 : 0;
						this.$emit('errCode', res.msg);
					}
					this.$emit('cbCode', res.event);
				})
			},
			/**
	         * 错误提示
	         */
			errMsg(status, msg) {
				switch(status) {
					case 1:
						return '用户不存在';
					case 2:
						return '该号码已注册';
					case 3:
						return '手机号码格式错误';
					case 1002:
						return '验证码发送次数已达上限';
					default:
						return `发送失败，${msg}`;
				}
			},
			/**
	         * 改变按钮点击状态，<0 不可点击，0 可点击
	         */
			changeStatus(status) {
				this.timer = status;
			}
		}
	}
</script>

<style scoped lang='less'>
	.el-button {
        width: 102px;
        padding: 10px 0;
        margin-left: 10px;
    }
</style>